package org.zoomdev.zoom.dao.adapters.result;

import org.zoomdev.zoom.common.json.JSON;
import org.zoomdev.zoom.dao.adapters.ResultSetAdapter;

import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StringJsonResultSetAdapter implements ResultSetAdapter {

    private final Type type;

    public StringJsonResultSetAdapter(Type type) {
        this.type = type;
    }

    @Override
    public Object getObject(ResultSet rs, int columnIndex) throws SQLException {
        String str = rs.getString(columnIndex);
        if (str == null) {
            return null;
        }
        return JSON.parse(str, type);
    }
}
